package spar.cancer.data.to.dump;

import spar.cancer.struct.CheckInfo;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DumpFile extends DumpAbstract{
    private static final String DIR_NAME = "output/checkinfo/";
    private final String fileName;

    public DumpFile(String fileName) {
        this.fileName = DIR_NAME + fileName;
        File file = new File(this.fileName);
        if (file.exists() && file.isFile()) {
            file.delete();
        } else {
            File dir = new File(DIR_NAME);
            dir.mkdirs();
        }
    }

    public DumpFile() {
        this(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".txt");
    }

    @Override
    public void dumpChkInfos(List<CheckInfo> checkInfos) {
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true)));
            for (CheckInfo info : checkInfos) {
                out.write(makeHash(info) + "\n");
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dumpLineCodes(Map<Integer, List<String>> lineCodes) {
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true)));
            for (Map.Entry<Integer, List<String>> entry : lineCodes.entrySet()) {
                out.write("{lineNumber=" + entry.getKey() + ", codes=" + entry.getValue() + "}\n");
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void close() {

    }
}
